﻿using UnityEngine;
using System.Collections;

public class ghost : SummonerSpell {

	// Use this for initialization
	void Start () {
		initializeStuff();
		initializeSummonerSpellStuff();
		forAllies = true;
		cardName = "Ghost";
		cardDescription = "Target allied Champion gains 2 additional moves this turn.";
	}
	
	public override void playSpell(Champion champion){
		champion.numGhostMoves = 2;
		champion.numMoves += 2;
		base.playSpell(champion);
	}
	
	public override float evaluateSummonerSpell(Champion champion){
		if(champion.numGhostMoves > 0)
			return 0;
		float hValue = 0;
		float xValue = 0;
		float yValue = 0;
		for(int i = -3; i < 4;i++){
			for(int j = -3; j < 4;j++){
				//is this move in range
				if((i+j)>0 && (i+j)<3){
					Vector2 newPosition = new Vector2(champion.myGridCell.gridPosition.x + i, champion.myGridCell.gridPosition.y + j);
					//is this move valid
					if(newPosition.x <= 3 && newPosition.x >= 0 && newPosition.y <= 3 && newPosition.y >= 0 && !GameController.gc.board[(int)newPosition.x,(int)newPosition.y].hasCard){
						float thisHValue = evalulateBoardMovement(champion, i, j);
						if(thisHValue > hValue){
							xValue = (int)newPosition.x;
							yValue = (int)newPosition.y;
							hValue = thisHValue;
						}
					}
				}
			}
		}
		if((xValue + yValue) > 1){
			return 10 * (xValue + yValue);
		}
		else{
			return 0;
		}
	}

	//evaluate the board after a move is made.
	float evalulateBoardMovement(Champion champion, float x, float y){
		float hValue = 0;
		foreach(Champion enemyChampion in GameController.gc.champions){
			if(enemyChampion!=null && enemyChampion.player1Card){
				float distance = Mathf.Abs(champion.myGridCell.gridPosition.x + x - enemyChampion.myGridCell.gridPosition.x) + Mathf.Abs(champion.myGridCell.gridPosition.y + y - enemyChampion.myGridCell.gridPosition.y);
				if(distance <= (enemyChampion.attackRange + enemyChampion.numMoves)) {
					if(champion.health <= enemyChampion.attack){
						hValue -= 10;
					}
					else{
						hValue += 10;
					}
				}
				else if(distance <= champion.attackRange){
					if(enemyChampion.health <= champion.attack){
						hValue += 10;
					}
					else
						hValue-=0;
				}
				else{
					float currentDistance = Mathf.Abs(champion.myGridCell.gridPosition.x - enemyChampion.myGridCell.gridPosition.x) + Mathf.Abs(champion.myGridCell.gridPosition.y - enemyChampion.myGridCell.gridPosition.y);
					if(currentDistance > distance){
						hValue += 8;
					}
					else{
						hValue -= 5;
					}
				}
			}
		}
		if(champion.myGridCell.gridPosition.x+x==3)
			hValue += 10;
		return hValue;
	}
}
